export default {
	data() {
		return{
			formLabelWidth:"150px",
			SRC:'',
			/*当前页显示数量*/
			pageSize:10,
			/*每页显示的数量*/
			pageSizes:[10, 20, 50, 100],
			/*dataTotal表数量*/
			dataTotal:0,
			/*分页器*/
			currentPage: 1,
			rules: {
				addUser: [
					{min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur,change' }
				],
				addMicro: [
					{min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur,change' }
				],
				addPhone: [
					{ pattern: /^[1]{1}[3|7|8|5]{1}[\d]{9}$/, message: '手机号格式错误', trigger: 'blur' }
				],
				addVoice: [
					{min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur,change' }
				],
				addMailbox: [
					{ type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur,change' }
				],
			},							// 修改弹窗出现
			revampShow: false,
			// 修改弹窗数据
			revampForm: {

			},
			// 验证 弹窗数据
			revamp: {

			},
			ruleForm: {
				addUser: '',
				addMicro: '',
				addPhone: '',
				addVoice: '',
				addMailbox: ''
			},
			revampForm: {
				addId : 0,
				addUser: '',
				addMicro: '',
				addPhone: '',
				addVoice: '',
				addMailbox: ''
			},
			tableHeader: [
				{"prop":"id","label":"序列号"},
				{"prop":"userName","label":"用户名"},
				{"prop":"phoneNum","label":"手机号-短信通知"},
				{"prop":"voiceNum","label":"语音号-语音通知"}, 
				{"prop":"weChat","label":"微信"},
				{"prop":"email","label":"邮箱"}
			],
			tableData: [],
			tableDataAll:[]
		} 
	},
	methods: {
		// 分页器
	    // 控制每页的数量
	    handleSizeChange(val) {
	      this.pageSize = val;
	      this.tableControl();
	    },
	    // 页数
	    handleCurrentChange(val) {
	      this.currentPage = val;
	      this.tableControl();
	    },
	    tableControl() {
	        this.tableData = this.tableDataAll.slice((this.currentPage-1)*this.pageSize,this.currentPage*this.pageSize)
	    },
		// 通用 成功 与 失败
	    successFun(data) {
	      this.$message({
	        message: data,
	        type: 'success'
	      });
	    },
	    errorFun(data) {
	      this.$message.error(data);
	    },
		handleClick () {
		},
		/*修改提交*/
		revampComit(formName) {
			this.$refs[formName].validate((valid) => {
				if (valid) {
					var d = this.revampForm;
					var obj = {
						id: d.addId,
						userName: d.addUser,
						weChat: d.addMicro,
						phoneNum: d.addPhone,
						voiceNum: d.addVoice,
						email: d.addMailbox
					}
					this.$http.post(`${this.SRC}monitoruser/modify.do`,obj,{emulateJSON:true}).then(function(res){
						var data = res.body;
						if(parseInt(data['code']) === 0){
							this.successFun(data['msg']);
							this.getTableData("");
						}else{
							this.errorFun(data['msg'])
						}
					})
				} else {
					return false;
				}
			});
		},
		findForm (formName) {
			this.$refs[formName].validate((valid) => {
				if (valid) {
					var d = this.ruleForm;
					var obj = {
						id: d.addId,
						userName: d.addUser,
						weChat: d.addMicro,
						phoneNum: d.addPhone,
						voiceNum: d.addVoice,
						email: d.addMailbox
					}
					obj.id = '';
					var str = '';
					for(var i in obj){
						if(obj[i] !== ""){
							str += `${i}=${obj[i]}&`
						}
					}
					str = str.substring(0,str.length-1);
					this.getTableData("?"+str);
				} else {
					return false;
				}
			});
		},
		addForm (formName) {
			this.$refs[formName].validate((valid) => {
				if (valid) {
					this.addUserData();
					this.getTableData("");
				} else {
					return false;
				}
			});
		},
		resetForm () {
			this.$refs[formName].resetFields();
		},
		edit(index, row) {
			this.revampForm.addId = row.id;
			this.revampForm.addUser = row.userName;
			this.revampForm.addMicro = row.weChat;
			this.revampForm.addPhone = row.phoneNum;
			this.revampForm.addVoice = row.voiceNum;
			this.revampForm.addMailbox = row.email;
			this.revampShow = true;
		},
		resetForm(formName) {
			this.$refs[formName].resetFields();
		},
		// 添加告警用户
		addUserData() {
			var obj = {
				userName: this.ruleForm.addUser,
				email: this.ruleForm.addMailbox,
				phoneNum: this.ruleForm.addPhone,
				voiceNum: this.ruleForm.addVoice,
				weChat: this.ruleForm.addMicro
			}
			this.$http.post(`${this.SRC}monitoruser/add.do`,obj,{emulateJSON:true}).then(function(res){
				var data = res.body;
				if(parseInt(data["code"]) === 0){
					this.successFun(data["msg"])
					this.getTableData("");
				}else{
					this.errorFun(data['msg'])
				}
			})
		},
		// 删除用户
	    handleDelete(index, row) {
	      var tableDataCP = this.tableData;
	      this.tableData.splice(index,1);
	      this.$http.get(`${this.SRC}monitoruser/delete.do?id=${row.id}`).then(function(res){
	      	var data = res.body
	      	if(parseInt(data['code']) === 0){
	      		this.successFun(data['msg']);
	      		this.getTableData("");
	      	}else{
	      		this.errorFun(data['code']);
	      		this.tableData = this.tableDataCP;
	      	}
	      })
	    },
		// 获取表格的数据
		getTableData(url) {
			this.$http.get(`${this.SRC}monitoruser/list.do${url}`).then((response) => {
				response = response.body;
				this.tableDataAll = response.msg;
				this.dataTotal = response.msg.length;
				this.tableControl();
			});
		}
	},
	created () {
		this.SRC = this.$store.getters.src3;
		this.getTableData("");
	}
} 